package com.cloudfun.campusshare.device.protocol.command;

import com.cloudfun.campusshare.util.ByteUtil;
import lombok.Data;
import org.apache.commons.lang3.ArrayUtils;

/**
 * Created by Huqin on 2019/12/27
 */
@Data
public class FrameBody {

    public static final byte FRAME_TYPE_CTRL = 1;
    public static final byte FRAME_TYPE_INFO = 2;

    /**
     * 默认地址标识
     */
    private static final byte[] DEFAULT_ADDRESS = new byte[]{0, 0, 0, 0, 0, 0};

    /**
     * 地址标识
     */
    private byte[] address;

    /**
     * 帧类型
     */
    private byte type;

    /**
     * 数据信息
     */
    private byte[] data;

    public FrameBody(byte type, byte[] data) {
        this.address = DEFAULT_ADDRESS;
        this.type = type;
        this.data = data;
    }

    public FrameBody(byte[] body) {
        this.address = ArrayUtils.subarray(body, 0, 6);
        this.type = body[6];
        this.data = ArrayUtils.subarray(body, 7, body.length);
    }

    public int length() {
        return address.length + 1 + data.length;
    }

    public byte[] toArray() {
        return ArrayUtils.addAll(ArrayUtils.addAll(address, type), data);
    }

    @Override
    public String toString() {
        return ByteUtil.bytesToHexString(this.toArray());
    }

}
